home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / announce.c next >
C/C++ Source or Header  |  1992-12-17  |  2KB  |  97 lines

  1. /*
  2.  *    announce - sits listening on a port, and whenever anyone connects
  3.  *           announces a message and disconnects them
  4.  *
  5.  *    Usage:    announce [port] < message_file
  6.  *
  7.  *    Author:    Lawrence Brown <lpb@cs.adfa.oz.au>     Aug 90
  8.  *
  9.  *    Bits of code are adapted from the Berkeley telnetd sources
  10.  */
  11.  
  12. #define PORT    4201
  13.  
  14. #include <sys/param.h>
  15. #include <sys/socket.h>
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. #include <signal.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. extern    char **environ;
  25. extern    int errno;
  26. char    *Name;            /* name of this program for error messages */
  27. char    msg[2048];
  28.      
  29. int
  30.   main(argc, argv)
  31. char *argv[];
  32. {
  33.   int s, ns, foo;
  34.   static struct sockaddr_in sin = { AF_INET };
  35.   char *host, *inet_ntoa();
  36.   char tmp[80];
  37.   long ct;
  38.   
  39.   Name = argv[0];        /* save name of program for error messages  */
  40.   sin.sin_port = htons((u_short)PORT);  /* Assume PORT */
  41.   argc--, argv++;
  42.   if (argc > 0) {        /*   unless specified on command-line       */
  43.     sin.sin_port = atoi(*argv);
  44.     sin.sin_port = htons((u_short)sin.sin_port);
  45.   }
  46.  
  47.   strcpy(msg, "");
  48.   strcpy(tmp, "");
  49.   while (1) {
  50.     if ((gets(tmp)) == NULL) break;
  51.     strcat(tmp, "\r\n");
  52.     strcat(msg, tmp);
  53.   }
  54.   msg[2048] = '\0';
  55.   signal(SIGHUP, SIG_IGN);    /* get socket, bind port to it      */
  56.   s = socket(AF_INET, SOCK_STREAM, 0);
  57.   if (s < 0) {
  58.     perror("announce: socket");;
  59.     exit(1);
  60.   }
  61.   if (bind(s, &sin, sizeof sin) < 0) {
  62.     perror("bind");
  63.     exit(1);
  64.   }
  65.   if ((foo = fork()) != 0) {
  66.     fprintf(stderr, "announce: pid %d running on port %d\n", foo,
  67.         ntohs((u_short)sin.sin_port));
  68.     exit(0);
  69.   } else {
  70.     setpriority(PRIO_PROCESS, getpid(), 10);
  71.   }
  72.   if (listen(s, 1) < 0) {        /* start listening on port */
  73.     perror("announce: listen");
  74.     exit(1);
  75.   }
  76.   foo = sizeof sin;
  77.   for(;;) {    /* loop forever, accepting requests & printing msg */
  78.     ns = accept(s, &sin, &foo);
  79.     if (ns < 0) {
  80.       perror("announce: accept");
  81.       exit(1);
  82.     }
  83.     host = inet_ntoa(sin.sin_addr.s_addr);
  84.     ct = time(0L);
  85.     fprintf(stderr, "CONNECTION made from %s at %s",
  86.         host, ctime(&ct));
  87.     write(ns, msg, strlen(msg));
  88.     sleep(5);
  89.     close(ns);
  90.   }
  91. }/* main */
  92.  
  93.  
  94.  
  95.  
  96.  
  97.